package com.jbm.jbmsupplier.frement;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.PledgeList;
import com.jbm.jbmsupplier.adapter.PledgeAdapter;
import com.jbm.jbmsupplier.adapter.PledgeAdapterList;
import com.jbm.jbmsupplier.presenters.PledgeLists;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.jbmsupplier.views.PledgeViews;
import com.jbm.onehome.iface.ISeller;
import com.jbm.onehome.iface.IorderEntity;
import com.jbm.onehome.iface.PaybackEntity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/11/7.
 */

 public class PledFrament extends Fragment implements PledgeViews, PledgeList.DatePleaMoeny{

    @InjectView(R.id.user_zje)
    TextView userZje;
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.lv_default1)
    ImageView lvDefault1;
    @InjectView(R.id.jinexianshi)
    TextView jinexianshi;


    private PledgeLists pledgeLists;
    private int page = 0;
    private PledgeAdapter adapter;
    private boolean isVisibleToUser1;
    static int tags = 0;
    private PledgeAdapterList adapterlist;
private int serverchang;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.lz_jinemingxi, null);
        ExitActivity.getInstance().addActivity(getActivity());
        ButterKnife.inject(this, view);
        pledgeLists = new PledgeLists(getContext(), this);
        if (tags == 0 && isVisibleToUser1 == true) {
            pledgeLists.getInit();
            jinexianshi.setText("总金额:");
        }
        pullUpRefresh();
        return view;
    }

    /**
     * 1，代表初始化获取数据成功 设置数据
     * 2，代表在请求数据进行更新
     *
     * @param arr
     * @param tag
     */
    @Override
    public void getData(List<PaybackEntity> arr, int tag) {//刷新数据
        if (tag == 1) {
            pullRefreshList.setAdapter(adapter = new PledgeAdapter(getContext(), arr));
        } else if (tag == 2) {
            adapter.setDataAll(arr);
        }
        if (arr.size() < 0) {
            lvDefault1.setVisibility(View.VISIBLE);
        }
        if (pullRefreshList != null) {
            pullRefreshList.onRefreshComplete();
        }
    }

    @Override
    public void getError(String name, DialogLoading dialogloading) {//失败
        if (dialogloading != null) {
            dialogloading.dismiss();
        }
        hint(name);
    }

    @Override
    public void getSucced(DialogLoading dialogloading) {
        if (dialogloading != null) {
            dialogloading.dismiss();

        }
    }


    @Override
    public void getUpdata() {
        ILoadingLayout startLabels = pullRefreshList.getLoadingLayoutProxy();
        startLabels.setRefreshingLabel("您已到底");// 刷新时
    }

    @Override
    public void getSuccedje(ISeller iSeller,int tags) {
        if (tags==1) {
            page = 0;
            if (iSeller.amount.equals("")) {
                userZje.setText("0");
            } else {
                userZje.setText(iSeller.amount + "");
            }
            pledgeLists.getRequData(page);

        }else if (tags==2){
            page = 0;
            if (iSeller.unpay_amount.equals("")) {
                userZje.setText("0");
            } else {
                userZje.setText(iSeller.unpay_amount + "");
            }
            serverchang=iSeller.service_charge;
            pledgeLists.getRequlist(page);


        }
    }
//月结的明细
    @Override
    public void getListData(List<IorderEntity> arrlist, int tag) {
        if (tag == 1) {
            pullRefreshList.setAdapter(adapterlist = new PledgeAdapterList(getContext(), arrlist,this));
        } else if (tag == 2) {
            adapterlist.setDataAll(arrlist);
        }
        if (arrlist.size() < 0) {
            lvDefault1.setVisibility(View.VISIBLE);
        }
        if (pullRefreshList != null) {
            pullRefreshList.onRefreshComplete();
        }




    }

    private void pullUpRefresh() {
        pullRefreshList.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {//预加载
                page += 10;
                if (tags==0){
                    pledgeLists.getRequData(page);
                }else if (tags==1){
                    pledgeLists.getRequlist(page);

                }

            }
        });
        pullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉
                page = 0;
                if (tags==0){
                    pledgeLists.getRequData(page);
                }else if (tags==1){
                    pledgeLists.getRequlist(page);

                }

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉
                page += 10;
                if (tags==0){
                    pledgeLists.getRequData(page);
                }else if (tags==1){

                    pledgeLists.getRequlist(page);
                }

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.user_zje, R.id.pull_refresh_list, R.id.lv_default1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_zje:
                break;
            case R.id.pull_refresh_list:
                break;
            case R.id.lv_default1:
                break;
        }
    }

    public void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(getContext(), name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setisVisibility();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisibleToUser1 = isVisibleToUser;
    }

    /**
     * 获取金额状态
     *
     * @param i
     * @param sta
     */
    @Override
    public void setDatePleaMoeny(int i, String sta) {
        tags = i;
        if (i == 0) {
            pledgeLists.getInit();
            jinexianshi.setText("总金额:");
        } else if (i == 1) {
            jinexianshi.setText("未结算金额:");
            pledgeLists.getYueJieList();
        }

    }

    @Override
    public int getServerChang() {
        return serverchang;
    }



}

